package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.view.ZoomImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ImageViewActivity extends BaseActivity {

    @BindView(R.id.iv_back_activity_image_view)
    ImageView mIvBackActivityImageView;
    @BindView(R.id.tv_title_activity_image_view)
    TextView mTvTitleActivityImageView;
    @BindView(R.id.viewpager_activity_image)
    ViewPager mViewpagerActivityImage;
    @BindView(R.id.tv_type_activity_image_view)
    TextView mTvTypeActivityImageView;
    @BindView(R.id.tv_details_activity_image_view)
    TextView mTvDetailsActivityImageView;
    @BindView(R.id.ll_botton_activity_image_view)
    LinearLayout mLlBottonActivityImageView;
    private int mChekcPosition;
    private ArrayList<String> mStringArrayListExtra;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_image_view);
    }

    /**
     * 户型图开启的方式
     *
     * @param context
     * @param stringList    数据
     * @param checkPosition 选中的图片顺序
     */
    public static void startActivity(Context context, ArrayList<String> stringList, int checkPosition) {
        Intent intent = new Intent(context, ImageViewActivity.class);
        intent.putStringArrayListExtra(Contans.INTENT_DATA, stringList);
        intent.putExtra("chekcPosition", checkPosition);
        context.startActivity(intent);
    }


    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        mLlBottonActivityImageView.setVisibility(View.GONE);
        mChekcPosition = intent.getIntExtra("chekcPosition", 0);
        mStringArrayListExtra = intent.getStringArrayListExtra(Contans.INTENT_DATA);
        ImagAdapter imagAdapter = new ImagAdapter();
        mViewpagerActivityImage.setAdapter(imagAdapter);
        mViewpagerActivityImage.setCurrentItem(mChekcPosition, false);
        mTvTitleActivityImageView.setText(mChekcPosition + 1 + "/" + mStringArrayListExtra.size());
        mViewpagerActivityImage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                mTvTitleActivityImageView.setText(position + 1 + "/" + mStringArrayListExtra.size());

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.iv_back_activity_image_view)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    private class ImagAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mStringArrayListExtra.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
          //  String s = mStringArrayListExtra.get(position);
       /* final     ZoomImageView zoomImageView = new ZoomImageView(ImageViewActivity.this);
            Glide.with(ImageViewActivity.this).load(s).asBitmap().into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                    int imageWidth = resource.getWidth();
                    int imageHeight = resource.getHeight();
                    int height =  UIUtils.getScreenWidth(ImageViewActivity.this) * imageHeight / imageWidth;



                    ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);


                    layoutParams.height = height;
                    layoutParams.width = UIUtils.getScreenWidth(ImageViewActivity.this);
                    zoomImageView.setImageBitmap(resource);



                }
            });*/

            ZoomImageView zoomImageView = new ZoomImageView(ImageViewActivity.this);
            String s = mStringArrayListExtra.get(position);
            zoomImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(ImageViewActivity.this).load(s).into(zoomImageView);
            container.addView(zoomImageView);
            return zoomImageView;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

}
